<?php
include "mysql_config.php";

set_time_limit(0);
date_default_timezone_set("Asia/Shanghai");
header("Content-Type: text/html; charset=UTF-8");
$mysqlConfig = new MysqlConfig();
$mysqli = new mysqli($mysqlConfig->getMysqlServerName(), $mysqlConfig->getMysqlUserName(), $mysqlConfig->getMysqlPassword(), $mysqlConfig->getMysqlDatabase());
//修改supermarket_coupon
$supermarketCouponSql = "select * from 91fbw_supermarket_coupon where status=0 and endtime < '". date('Y-m-d') . "';";
$supermarketCouponResult = $mysqli->query($supermarketCouponSql);
while ($row = mysqli_fetch_assoc($supermarketCouponResult)) {
    $supermarketCouponId = $row['id'];
    $updateSql = "update 91fbw_supermarket_coupon set status=2 where id=" . $supermarketCouponId;
    $mysqli->query($updateSql);
}

//更新supermarket_config
$supermarketConfigSql = "select * from 91fbw_supermarket_config where status != 'wait_open' and status != 'wait_show' ";
$supermarketConfigResult = $mysqli->query($supermarketConfigSql);
while ($row = mysqli_fetch_assoc($supermarketConfigResult)) {
    $cityId = $row['cityid'];
    $freeCouponNumber = $row['freecouponnumber'];
    $rechargeCouponNumber = $row['rechargecouponnumber'];
    $effectiveTime = $row['effectivetime'];
    $status = $row['status'];
    //体验券发光了
    $isCloseSend = 0;
    $freeSendNumber = 0;
    $rechargeSendNumber = 0;
    $freeSupermarketCouponSql = "select count(*) as count from 91fbw_supermarket_coupon where cityid = " . $cityId . " and type='free' and created_at>='".$effectiveTime."';";
    $freeSupermarketCouponResult = $mysqli->query($freeSupermarketCouponSql);
    while ($freeSupermarketCouponRow = mysqli_fetch_assoc($freeSupermarketCouponResult)) {
        $freeSendNumber = $freeSupermarketCouponRow['count'];
        $freeSendNumber = $freeSendNumber ? $freeSendNumber :0;
    }
    $rechargeSupermarketCouponSql = "select count(*) as count from 91fbw_supermarket_coupon where cityid = " . $cityId . " and type='recharge' and created_at>='".$effectiveTime."';";
    $rechargeSupermarketCouponResult = $mysqli->query($rechargeSupermarketCouponSql);
    while ($rechargeSupermarketCouponRow = mysqli_fetch_assoc($rechargeSupermarketCouponResult)) {
        $rechargeSendNumber = $rechargeSupermarketCouponRow['count'];
        $rechargeSendNumber = $rechargeSendNumber ? $rechargeSendNumber : 0;
    }
    if($status == 'close_send') {
        $isCloseSend = 1;
    } else {
        if (($rechargeCouponNumber == $rechargeSendNumber) && ($freeSendNumber == $freeCouponNumber)) {
            $isCloseSend = 1;
        }
    }
    //券发光了，并且所有券都已使用或过期了
    $judgeUserOrOverdueStatus = 0; //默认还有没有使用过的体验券
    $judgeUserOrOverdueStatusSql = "select count(*) as count from 91fbw_supermarket_coupon where status=0 and cityid=".$cityId . " and created_at>='". $effectiveTime . "';";
    $judgeUserOrOverdueStatusResult = $mysqli->query($judgeUserOrOverdueStatusSql);
    while ($row = mysqli_fetch_assoc($judgeUserOrOverdueStatusResult)) {
        if($row['count'] == 0) {
            $judgeUserOrOverdueStatus = 1;
        }
    }
    if($isCloseSend && !$judgeUserOrOverdueStatus) {
        //体验券发送已完成, 但还有没有使用的券
        if($status != 'close_send') {
            $updateSupermarketConfigStatusSql = "update 91fbw_supermarket_config set status='close_send' , updated_at='".date('Y-m-d H:i:s')."' where cityid = " . $cityId;
            $mysqli->query($updateSupermarketConfigStatusSql);
            $insertSupermarketLogSql = "insert into 91fbw_supermarket_log (`cityid`,`type`,`content`,`remark`,`adminid`,`created_at`,`updated_at`)" .
                " values(" . $cityId . ",'close_send', 'close send coupon ', ' ', 0, '" . date('Y-m-d H:i:s') . "', '" . date('Y-m-d H:i:s') . "')";
            $mysqli->query($insertSupermarketLogSql);
        }
    } elseif($isCloseSend && $judgeUserOrOverdueStatus) {
        //体验券发送已完成, 且没有使用的券了
        if($status !=  'close') {
            $firstClassId = 6;
            $updateSupermarketConfigStatusSql = "update 91fbw_supermarket_config set status='close' , updated_at='".date('Y-m-d H:i:s')."'where cityid = " . $cityId;
            $mysqli->query($updateSupermarketConfigStatusSql);
            $shopIdsSql = "select * from 91fbw_shops where status=1 and cityid=" .$cityId . " and firstclassid=". $firstClassId . ";";
            $shopIdsResult = $mysqli->query($shopIdsSql);
            while ($shopIdRow = mysqli_fetch_assoc($shopIdsResult)) {
                $shopId = $shopIdRow['id'];
                $insertShopLogSql = "insert into 91fbw_shop_check(`shopid`,`type`,`withholdtype`,`reason`,`remark`,`contractorid`,`contractorname`,`maintainerid`,`maintainername`,`adminid`,`created_at`, `updated_at`)" .
                    " value(".$shopId.", 'automatic_downshelf_supermarket', 0, 'automatic_downshelf_supermarket', 'automatic_downshelf_supermarket', ".$shopIdRow['maintainerid'].", '2', ".
                    $shopIdRow['maintainerid']. ", '2', 2, '".date('Y-m-d H:i:s')."', '".date('Y-m-d H:i:s')."');";
                $mysqli->query($insertShopLogSql);
            }
            $updateShopStatusSql = "update 91fbw_shops set status=0, updated_at='".date('Y-m-d H:i:s')."' where status=1 and cityid=" .$cityId." and firstclassid=" . $firstClassId . ";";
            $mysqli->query($updateShopStatusSql);
            $insertSupermarketLogSql = "insert into 91fbw_supermarket_log (`cityid`,`type`,`content`,`remark`,`adminid`,`created_at`,`updated_at`)" .
                " values(" . $cityId . ",'close', 'close vip', ' ', 0, '" . date('Y-m-d H:i:s') . "', '" . date('Y-m-d H:i:s') . "')";
            $mysqli->query($insertSupermarketLogSql);
        }
    }
}

$mysqli->close();
